package top.healthylife.gateway.platform.td.api.api.creditbodyguard;

import cn.hutool.core.util.ObjectUtil;
import com.alibaba.fastjson.JSONObject;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import top.healthylife.basics.utils.exception.BusinessException;
import top.healthylife.gateway.core.service.ParentFace;
import top.healthylife.gateway.dto.annotation.ApiServiceExplain;
import top.healthylife.gateway.platform.td.api.utils.TdUtils;
import top.healthylife.gateway.platform.td.dto.creditbodyguard.zhichascore.req.TdReqZhiChaScoreDto;
import top.healthylife.gateway.platform.td.dto.creditbodyguard.zhichascore.res.TdResAntifraudModel;
import top.healthylife.gateway.platform.td.dto.creditbodyguard.zhichascore.res.TdResAntifraudModelItem;
import top.healthylife.gateway.platform.td.dto.creditbodyguard.zhichascore.res.TdResZhiChaScoreDto;

/**
 * @author : GaoZeXi
 * @date 2021/7/28 9:48
 */
@Slf4j
@Service
@ApiServiceExplain(price = 20, apiName = "智察分", info = "信贷保镖紧密围绕客户的业务和风控需求，为银行信用卡、现金贷、消费金融、汽车金融等各行各业的客户提供全生命周期的一站式定制服务，并采用项目模式提供咨询、数据、产品、运营等协同服务。全力协助客户在保证业务质量的同时提高业务效率，规避业务风险，最终实现盈利目标。")
public class TdBusinessFlowZhiChaScoreApi implements ParentFace<TdReqZhiChaScoreDto, TdResZhiChaScoreDto> {
    static final String URL = "/bodyguard/apply/v5";
    static final String METHOD = "POST";
    static final String BIZ_CODE = "jck_jck_zcf";
    static final String MODULE_NAME = "ANTIFRAUD_MODEL";
    @Override
    public TdResZhiChaScoreDto execute(TdReqZhiChaScoreDto t) throws Exception {
        JSONObject jsonObject = TdUtils.tongDunHttpRequest(t, URL, METHOD, BIZ_CODE);
        try {
            TdResZhiChaScoreDto tdResZhiChaScoreDto = jsonObject.toJavaObject(TdResZhiChaScoreDto.class);
            if(ObjectUtil.isEmpty(jsonObject.getString(TdUtils.REASON_DESC))){
                TdResAntifraudModel resultDesc = tdResZhiChaScoreDto.getResultDesc();
                JSONObject jsonObject1 = jsonObject.getJSONObject(TdUtils.RESULT_DESC).getJSONObject(MODULE_NAME);
                String next = jsonObject1.keySet().iterator().next();
                JSONObject jsonObject2 = jsonObject1.getJSONObject(next);
                TdResAntifraudModelItem antifraudModel = jsonObject2.toJavaObject(TdResAntifraudModelItem.class);
                antifraudModel.setModelNodeFlagNo(next);
                resultDesc.setAntifraudModel(antifraudModel);
                tdResZhiChaScoreDto.setResultDesc(resultDesc);
            }
            return tdResZhiChaScoreDto;
        }catch (Exception e){
            throw new BusinessException("同盾智察分结果解析异常");
        }
    }
}
